package cn.hxlc.tie.demo.modular.zr.equipmentzr.controller;/*
 @author rui
 @date 2021/6/29 --11:13
*/



import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.param.EquipmentGroupParam;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.service.EquipmentGroupService;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.web.response.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_ERROR_CODE;
import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

@RestController
@ApiResource(name="分组树",path = "/groupTree")
public class ZrEquipmentGroupController {

    @Autowired
    private EquipmentGroupService equipmentGroupService;
    /**
     * 获取分组树列表
     *
     * @Date 2020-12-14
     */
    @GetResource(name = "查询分组树", path = "/queryGroupTree", requiredPermission = false)
    @ApiOperation(value = "查询分组树")
    public ResponseData queryGroupTree() {
        return ResponseData.success(this.equipmentGroupService.queryTree());

    }
   /*
   * 查询列表
   *
   * */
    @GetResource(name="查询列表",path="/queryList",requiredPermission=false)
    @ApiOperation(value="查询列表")
    public ResponseData queryList(EquipmentGroupParam equipmentGroupParam){
    return ResponseData.success(this.equipmentGroupService.queryListOne(equipmentGroupParam));
    }

    @PostResource(name="新增分组",path = "/add",requiredLogin = false,requiredPermission = false)
    @ApiOperation(value = "新增")
    public ResponseData addTree(@RequestBody EquipmentGroupParam equipmentGroupParam){
        equipmentGroupService.add(equipmentGroupParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"新增成功！",null);
    }


    @GetResource(name = "根据父id获取详情", path = "/queryOneByPid")
    public ResponseData queryOneByPid(@RequestParam Long pid) {
        return ResponseData.success(equipmentGroupService.getGroupById(pid));
    }

    @PostResource(name="修改分类",path="/updateOne",requiredLogin = false,requiredPermission=false)
    @ApiOperation(value="修改")
    public ResponseData updateTree(@RequestBody EquipmentGroupParam equipmentGroupParam){
        if (equipmentGroupParam.getFstatus()!=null){
            if (equipmentGroupParam.getFstatus()==0){

                return ResponseData.error(DEFAULT_ERROR_CODE,"存在已启用数据修改失败！",null);
            }
        }
        equipmentGroupParam.setFstatus(1);
        equipmentGroupService.update(equipmentGroupParam);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"修改成功",null);
    }

    @PostResource(name="删除分类",path="/deleteOne",requiredLogin = false,requiredPermission = false)
    @ApiOperation("删除")
    public ResponseData deleteTree(@RequestBody EquipmentGroupParam equipmentGroupParam){
        equipmentGroupService.delete(equipmentGroupParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE, "删除成功!",null);
    }
}
